package com.sd.reader;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.alex.books.BookPageFactory;
import com.sd.activity.R;

/**
 * 
 * 
 * @author Alex.Lu
 *
 */
public class ReaderFlipper extends ViewFlipper implements OnGestureListener, OnTouchListener, OnClickListener {

	private final String TAG = "HorizontalFlipper";
	
	public static final int UPDOWN_DIR = 0;
	public static final int RIGHTLEFT_DIR = 1;
	private int mFliperDirection;
	private Context mContext;
	private GestureDetector mDetector;
	/**
	 * 当前
	 */
	private TextView mCurText;
	/**
	 * 
	 */
	private TextView mNextText;
	private OnClickListener mOnClickListener;
	private TextView mTitleTV;
	private int count;
	
	public ReaderFlipper(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initView(context);
	}
	
	public ReaderFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		 initView(context);
	}
	
	public void setmTitleTV(TextView titleTV){
		mTitleTV = titleTV;
	}
	private void initView(Context context){
		mFliperDirection = RIGHTLEFT_DIR;
		mContext = context;
		mDetector = new GestureDetector(this);
		this.setOnTouchListener(this);
		this.setOnClickListener(this);
		mCurText = new TextView(mContext);
		mCurText.setTextColor(0xff000000);
		addView(mCurText);
		
		mNextText = new TextView(mContext);
		mNextText.setTextColor(0xff000000);
		addView(mNextText);
		//BookPageFactory.getInstance().setViews(mCurText,mNextText);
	}

	public void setReaderTextSize(float size){
		mNextText.setTextSize(size);
		mCurText.setTextSize(size);
		BookPageFactory.getInstance().setFontSize(size * 3 / 2);
	}
	
	public void reFreshPage(){
		String text = BookPageFactory.getInstance().refreshPage();
		if(count%2!=0){
			mCurText.setText(text);
		}else{
			mNextText.setText(text);
		}
	}
	
	public void setLineSpace(float size){
		BookPageFactory.getInstance().setLineSpace(size);
		mNextText.setLineSpacing(size, 1);
		mCurText.setLineSpacing(size, 1);
	}
	/**
	 * 设置背景色
	 * @param color
	 */
	public void setReaderBg(int color){
		this.setBackgroundColor(color);
	}
	/**
	 * 设置字体颜色
	 * @param color
	 */
	public void setViewColor(int color){
		mCurText.setTextColor(color);
		mNextText.setTextColor(color);
	}
	
	/**
	 * 设置切换的方向
	 * @param dir 
	 */
	public void setReaderDir(int dir){
		mFliperDirection = dir;
	}
	
	/**
	 * 上下切换
	 * @param e1
	 * @param e2
	 * @param velocityX
	 * @param velocityY
	 * @return
	 */
	private boolean updownFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){
		boolean flag = false;
		if (e1.getY() - e2.getY() > 50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_up_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_up_out));
			flag = nextPage();
			if(flag){
				this.showNext();
			}
			return flag;
		} else if (e1.getY() - e2.getY() < -50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_down_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_down_out));
			flag = prePage();
			if(flag){
				this.showNext();
			}
			return flag;
		}
		return false;
	}
	
	
	private boolean rightleftFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){
		boolean flag = false;
		if (e1.getX() - e2.getX() > 50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_left_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_left_out));
			flag = nextPage();
			if(flag){
				this.showNext();
			}
			return flag;
		} else if (e1.getX() - e2.getX() < -50) {
			this.setInAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_right_in));
			this.setOutAnimation(AnimationUtils.loadAnimation(mContext,R.anim.push_right_out));
			flag = prePage();
			if(flag){
				this.showNext();
			}
			return flag;
		}
		
		return false;
	}
	
	public boolean nextPage(){
		
		//String text = BookPageFactory1.getInstance().nextPage();
		
		if(BookPageFactory.getInstance().isLastPage()){
			Toast.makeText(mContext, "已经是最后一页了", Toast.LENGTH_SHORT).show();
			return false;
		}else{
			String text = BookPageFactory.getInstance().nextPage();
			String name = BookPageFactory.getInstance().getCurChapter().mChapterName;
			mTitleTV.setText(name);
			if(count%2==0){
				mCurText.setText(text);
			}else{
				mNextText.setText(text);
			}
			count++;
			return true;
		}
	}
	
	/*public void curPage(){

		String text = BookPageFactory1.getInstance().rePage();
		if(count%2!=0){
			mCurText.setText(text);
		}else{
			mNextText.setText(text);
		}
	}*/
	
	private boolean prePage(){
		//String text = BookPageFactory1.getInstance().previousPage();
		
		if(BookPageFactory.getInstance().isFirstPage()){
			Toast.makeText(mContext, "已经是第一页了", Toast.LENGTH_SHORT).show();
			return false;
		}else{
			String text = BookPageFactory.getInstance().previousPage();
			mTitleTV.setText(BookPageFactory.getInstance().getCurChapter().mChapterName);
			if(count%2==0){
				mCurText.setText(text);
			}else{
				mNextText.setText(text);
			}
			count++;
			return true;
		}
		
	}
	
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(mFliperDirection == UPDOWN_DIR){
			updownFling(e1,e2,velocityX,velocityY);
		}else if(mFliperDirection == RIGHTLEFT_DIR){
			rightleftFling(e1,e2,velocityX,velocityY);
		}
		return false;
	}

	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		mOnClickListener.onClick(this);
		return false;
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		mDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	
	public void setOnSingleTapUpListener(OnClickListener onClickListener){
		mOnClickListener = onClickListener;
	}
	
}
